package com.shou.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.shou.controller.MyController;
import com.shou.entity.SlideShow;
import com.shou.service.impl.SlideShowService;

@Controller("adminSlideShowController")
@RequestMapping("/admin/slideShow")
public class SlideShowController extends MyController{

	@Autowired
	private SlideShowService slideShowService;
	
	@RequestMapping("/list")
	public String slideShowList(Model model){
		List<SlideShow> list = slideShowService.slideShowList();
		model.addAttribute("list", list);
		return "/admin/slideShow/list";
	}
	
	@RequestMapping("/edit")
	public String editSlideShow(Integer slideId,Model model) {
		if(slideId!=null){			
			SlideShow slide = slideShowService.getSlideShowInfo(slideId);
			model.addAttribute("slideShow", slide);
		}
		return "/admin/slideShow/edit";
	}
	
	@RequestMapping("/saveSlideShow")
	public ResponseEntity<String> saveSlideShow(SlideShow slideShow){
		String msg = "保存成功";
		boolean status = true;
		try{			
			slideShowService.editSlideShow(slideShow);
		}
		catch(Exception e) {
			msg = "保存失败";
			status = false;
			e.printStackTrace();
		}
		return renderMsg(status, msg);
	}
	
	@RequestMapping("/uploadSlideShow")
	public ResponseEntity<String> uploadSlideShow(@RequestParam("cmfile") CommonsMultipartFile cmfile, Integer fsize,HttpServletResponse response) {
		String msg = "上传成功";
		boolean status = true;
		Map<String,String> map = new HashMap<>();
		try{			
			String filePath = slideShowService.uploadSlideImage(cmfile, fsize);
			map.put("filePath", filePath);
		}
		catch(Exception e){
			e.printStackTrace();
			status = false;
			msg = "上传失败";
		}
		return renderData(status, msg,map);
	}
}
